﻿using ClockLib;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ClockServer.Models
{
    public class ClockSettingsDto
    {
        public string ReportMinutes { get; set; } 
        public string SilentHours { get; set; }
        public bool EnableBellRing { get; set; } 

        public int BellRepeatTime { get; set; } 

        public bool EnableTimeReport { get; set; }
        public int CheckPeriod { get; set; } 

        public string ReportTemplate { get; set; }

        public ClockSettingsDto()
        {

        }
        public ClockSettingsDto(ClockSettings settings)
        {
            ReportMinutes = string.Join(' ', settings.ReportMinutes);
            SilentHours = string.Join(' ', settings.SilentHours);
            EnableBellRing = settings.EnableBellRing;
            EnableTimeReport = settings.EnableTimeReport;
            BellRepeatTime = settings.BellRepeatTime;
            ReportTemplate = settings.ReportTemplate;
            CheckPeriod = settings.CheckPeriod;
        }
        public bool Update(ClockSettings settings)
        {
            try
            {
                var minutes = this.ReportMinutes.Split(' ').Select(ch => int.Parse(ch)).OrderBy(n=>n).ToHashSet().ToArray();
                settings.ReportMinutes = minutes;
                var hours = this.SilentHours.Split(' ').Select(ch => int.Parse(ch)).OrderBy(n => n).ToHashSet().ToArray();
                settings.SilentHours = hours;
                settings.EnableTimeReport = this.EnableTimeReport;
                settings.BellRepeatTime = this.BellRepeatTime;
                settings.EnableBellRing = this.EnableBellRing;
                settings.ReportTemplate = this.ReportTemplate;
                return true;
            }
            catch (Exception)
            {
                throw;
                return false;
            }

        }

        //private bool CompareAndUpdate<T>(ref T left, ref T right)
        //{
        //    if (left is IComparable cmp1 && right is IComparable cmp2)
        //    {
        //        if (cmp1.CompareTo(cmp2)==0)
        //        {
        //            return false;
        //        }
        //        left=
        //    }
        //}
    }
}
